package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

// stu url : https://www.jianshu.com/p/d059c21085fa

func CreateFile(path string) {
	f, err := os.Create(path) // 以覆盖的方式创建文件
	if err != nil {
		panic(err)
	}
	defer f.Close()
}

func DeleteFile(path string) {
	if err := os.Remove(path); err != nil {
		panic(err)
	}
}

func ReadFile(path string) {
	// 逐行读
	f, err := os.Open(path)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	endBs := []byte{}
	for {
		bs := make([]byte, 1024)
		n, err := f.Read(bs)
		if n == 0 || err == io.EOF {
			fmt.Println("读取结束")
			break
		}
		endBs = append(endBs, bs[:n]...)
	}
	fmt.Printf("%s", endBs)
}

func WriterFile(path, body string) {
	f, err := os.Create(path)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	f.Write([]byte(body))
}

func CopyFile(path1, path2 string) {
	f1, err := os.Open(path1)
	if err != nil {
		panic(err)
	}
	defer f1.Close()
	f2, err := os.Create(path2)
	if err != nil {
		panic(err)
	}
	defer f2.Close()
	io.Copy(f2, f1)
}

func ReadFile2(path string) {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s", data)
}

func WriteFile2(path string, data string) {
	// 0666 这个是和linux给文件授权是一样的, 0 表示10进制， 6分别代表 用户 组用户 其他用户
	// 该方法有则重写，无则创建
	if err := ioutil.WriteFile(path, []byte(data), 0666); err != nil {
		panic(err)
	}
}

func ReadFile3(path string) {
	// bufio.NewReader 具有缓冲作用，对于大文件一次性全部读入对内存负担较大
	f, err := os.Open(path)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	bf := bufio.NewReader(f)
	bs := make([]byte, 1024)
	for {
		n, err := bf.Read(bs)
		if n == 0 || err == io.EOF {
			fmt.Println("读完")
			break
		}
		fmt.Printf("%s", bs[:n])
	}
}

func WriteFile3(path string, data string) {
	// 有缓冲的写
	f, err := os.Create(path)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	bf := bufio.NewWriter(f)
	bf.WriteString(data)
	bf.Flush() // 将缓存的写入的数据，真正提交入文件
}

func WriteFileApp(path string, data string) {
	f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	bf := bufio.NewWriter(f)
	bf.WriteString(data)
	bf.Flush()
}

func WriteFileApp2(path string, data string) {
	f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	f.WriteString(data)
}

func WriteFileApp3(path string, data string) {
	f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	dataReader := strings.NewReader(data)
	io.Copy(f, dataReader)
}

func main() {
	// 1. 创建文件
	// CreateFile("./test")

	// 2. 删除文件
	// DeleteFile("./test")

	// 3. 读取文件操作
	// ReadFile("./read.txt")
	// ReadFile2("./read.txt")
	// ReadFile3("./read.txt")

	// 4. 写入文件
	// WriterFile("./writer.txt", "hello, my son")
	// WriteFile2("./writer2.txt", "hello, golang yyds")
	// WriteFile3("./writer3.txt", "hello, python")

	// 5. 复制文件
	// CopyFile("./read.txt", "./read_copy.txt")

	// 6. 追加写入
	// WriteFileApp("./writer3.txt", "hello, c++")
	// WriteFileApp2("./writer3.txt", "\nhello, Java")
	// WriteFileApp3("./writer3.txt", "\nhello, Java2se")
}
